package com.inews.business.web;


import java.io.InputStream;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import com.inews.base.mapper.JsonMapper;
import com.inews.base.utils.InewsUtils;
import com.inews.base.utils.Servlets;
import com.inews.base.vo.ExtData;

import com.inews.business.service.IPostChangeService;

import com.inews.business.vo.PostChangeVo;

import com.inews.business.vo.TeacherVo;

@Controller
@RequestMapping(value="/postchange")
public class PostChangeController {
	@Autowired
	private IPostChangeService service;
	
	/**
	 * 分页查询退休人员历史.
	 *
	 * @param request the request
	 * @param pageNumber the page number
	 * @param sort the sort
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("findPostChangePage")
	@ResponseBody
	public Object findPostChangePage(ServletRequest request, @RequestParam(value = "page", defaultValue = "1") int pageNumber, String sort, TeacherVo vo){
		Pageable page = Servlets.buildPageRequest(pageNumber, sort);
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		ExtData<PostChangeVo> extData = service.findPostChangePage(searchParams, page);
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
//	
	
	/**
	 * 保存退休人员信息.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("savePostChange")
	@ResponseBody
	
	public Object savePostChange(PostChangeVo vo){
		ExtData<PostChangeVo> extData = service.savePostChange(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
//
//	
//	
	/**
	 * 更新退休人员信息.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("updatePostChange")
	@ResponseBody
	public Object updatePostChange(PostChangeVo vo){
		ExtData<PostChangeVo> extData = service.updatePostChange(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
//	
//	
	/**
	 * 删除退休人员信息.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("deletePostChange")
	@ResponseBody
	public Object deletePostChange(PostChangeVo vo){
		ExtData<PostChangeVo> extData = service.deletePostChange(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	/**
	 * 报表导出.
	 *
	 * @return the model and view
	 */
	@RequestMapping("exportPostChangeExcel")
	public String exportPostChangeExcel(){
		return "postChangeExcelView";
	}
	
	
	/**
	 * 报表导入.
	 *
	 * @return the model and view
	 */
	@RequestMapping("importExcel")
	public ResponseEntity<String> importExcel(@RequestParam("excelFile") MultipartFile excelFile){
		InputStream is;
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.setContentType(MediaType.TEXT_HTML);
		try {
			is = excelFile.getInputStream();
			ExtData<String> extData = service.importExcel(is, InewsUtils.getUserInfo());
			String json = JsonMapper.nonEmptyMapper().toJson(extData);		
		
			return new ResponseEntity<String>(json, responseHeaders, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			ExtData result=new ExtData();
			result.setSuccess(false);
			result.setError(e.getMessage());
			String json = JsonMapper.nonEmptyMapper().toJson(result);			
			return new ResponseEntity<String>(json, responseHeaders, HttpStatus.EXPECTATION_FAILED);
			//throw new ExlImportException("导入失败");
		}
	}
}
